package com.models;

import java.util.ArrayList;






import java.util.List;

import net.sf.json.JSONObject;

import com.om.base.AbstractBean;
import com.om.base.SqlFilter;
import com.om.base.Utils;
import com.om.common.BaseBean;

public class Controls extends BaseBean {
	
	public Controls(){
		this.TABLE_NAME="requirement_item";
		this.PrimaryKey = "ri_id";	
		this.addChild("ri_id", "ri_id", ControlItems.class);
		this.addChild("GuidCode", "ParentGuidCode", ControlsCombine.class);
	}
	
	/**
	 * 重載afterLoad方法，把擴展配置extend_config由字符串轉為Json對象
	 * @throws Exception 
	 */
	@Override
	public void afterLoad() throws Exception{
		if(!Utils.IsNullOrEmpty(this.getStringValue("extend_config"))){
			JSONObject jsonObj =JSONObject.fromObject(this.getStringValue("extend_config"));
			this.set("extend_config", jsonObj);			
		}
		super.afterLoad();
	}
	
	/**
	 * 重載deleteItem方法，把active設置為0
	 */
	@Override
	public void deleteItem() throws Exception {
		this.set("active", 0);
		this.updateItem();
	}
	
	/**
	 * 重載getChild方法，只加載acitve=1的子表數據且按照主键升序排列
	 */
	/*@Override
	public List<AbstractBean<T>> getChild (Class<T> childClass) {
		String orderBy="";
		if(childClass.equals(ControlItems.class)){
			orderBy ="order by rid_id Asc";
		}
		else{
			orderBy ="order by sort Asc";
		}
		return super.getChild(childClass,new SqlFilter("active",SqlFilter.OP.EQUALS,1),orderBy);
	}
	
	@Override
	public boolean save() throws Exception {
		int index=1;
		ArrayList<AbstractBean>combineList=this.getChild(ControlsCombine.class);		
		for(int i=0;i<combineList.size();i++){
			AbstractBean child = combineList.get(i);
			if(!child.getStringValue(AbstractBean.DATA_ROW_STATE).equals(AbstractBean.DELETED)){
				child.set("sort", index);
				index++;
			}
		}
		return super.save();
	}*/
	

}
